<?php


function checkboxes($params)
{

    $name = '';
    $values = null;
    $options = null;
    $selected = null;
    $separator = '';
    $labels = true;
    $output = null;
    $class = '';
    $object_value = false;
    $object_key = false;
    $binary_selected = false;

    $extra = '';

    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'binary_selected':
                $$_key = $_val;
                break;
            case 'object_value':
            case 'object_key':
                $$_key = str_replace('$', '$_val->', (string)$_val);
                break;
            case 'class':
                $$_key = " {$_key}=\"{$_val}\"";
                break;
            case 'name':
            case 'separator':
                $$_key = $_val;
                break;

            case 'labels':
                $$_key = (bool)$_val;
                break;
            case 'options':
                if (is_a($_val, 'ActiveRecordCollection') || is_a($_val, 'Collection')) {
                  $$_key = $_val;
                } else {
                  $$_key = (array)$_val;
                }
                break;
                
            case 'values':
            case 'output':
                if (is_a($_val, 'Collection')) {
                  $$_key = array_values($_val->to_array());
                } else {
                  $$_key = array_values((array)$_val);
                }
                break;

            case 'checked':
            case 'selected':
                $selected = array_map('strval', array_values((array)$_val));
                break;

            case 'assign':
                break;

            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.($_val).'"';
                } else {
                    $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }

    if (!isset($options) && !isset($values))
        return ''; /* raise error here? */

    settype($selected, 'array');
    $_html_result = array();

    if (isset($options)) {

        foreach ($options as $_key => $_val) {
            if ($object_value) {
              $code = "\$value = \"{$object_value}\";";
              eval ($code);
            } else {
              $value = $_val;
            }
            if ($object_key) {
              $code = "\$key = \"{$object_key}\";";
              eval ($code);
            } else {
              $key = $_key;
            }
            $_html_result[] = smarty_function_html_checkboxes_output($name, $key, $value, $selected, $extra, $separator, $labels, $class, $binary_selected);
          }


    } else {
        foreach ($values as $_i=>$_key) {
            $_val = isset($output[$_i]) ? $output[$_i] : '';
            $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $class, $binary_selected);
        }

    }

    if(!empty($params['assign'])) {
        $smarty->assign($params['assign'], $_html_result);
    } else {
        return implode("\n",$_html_result);
    }

}

function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $class, $binary_selected) {
    $_output = '';
    if ($labels) $_output .= "<label{$class}>";
    $_output .= '<input type="checkbox" name="'
        . ($name) . '[]" value="'
        . ($value) . '"';
		
    if (($binary_selected) && ($binary_selected & $value)) {
        $_output .= ' checked="checked"';
    } elseif (in_array((string)$value, $selected)) {
        $_output .= ' checked="checked"';
    }
    
    $_output .= $extra . ' />' . $output;
    if ($labels) $_output .= '</label>';
    $_output .=  $separator;

    return $_output;
}

?>